home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Documents / Other / KBNS.verification.32.1 / KBNS.11.3.011 / DOLeakTest_main.m next >
Text File  |  1993-08-25  |  3KB  |  105 lines

  1. /*
  2.  *  Distributed Objects memory leak test program.
  3.  *
  4.  *  To use this program, first compile it as follows:
  5.  *
  6.  *  cc -Wall -g DOLeakTest_main.m -lMallocDebug -o DOLeakTest_Main 
  7.  *
  8.  * To run the program, start the first one with the FULL path
  9.  * name in a shell. This becomes the server.
  10.  *
  11.  * Select this "server" from the MallocDebug.app and check for
  12.  * leaks, by clicking the "Leaks" button. It should say "No nodes"
  13.  * at this point.
  14.  *
  15.  * Now, start another one. This
  16.  * will be the client which will send the argument string over
  17.  * to the server. At this point, no leaks should occur. The next time
  18.  * you send a string, the previous string will become the leaked memory.
  19.  *
  20.  * Somebody please test this code under NEXTSTEP 3.0 and NEXTSTEP 3.1 to
  21.  * see if this BUG has been fixed under 3.1.
  22.  *
  23.  * Thanks.
  24.  */
  25.  
  26.  
  27.  
  28.  
  29. #import <objc/objc.h>
  30. #import <objc/Object.h>
  31. #import <remote/NXProxy.h>
  32. #import <remote/NXConnection.h>
  33. #import <stdio.h>
  34. #import <string.h>
  35.  
  36. #define RO_NAME        "leak_test"
  37.  
  38.  
  39. @protocol ServerProtocol
  40. - receiveStr:(const char *)aStr;
  41. @end
  42.  
  43. @interface MyServer:Object {}
  44. - receiveStr:(const char *)aStr;
  45. @end
  46.  
  47.  
  48. @implementation MyServer:Object
  49. - receiveStr:(const char *)aStr;
  50. {
  51.     fprintf(stderr, "Received '%s'\nThis will not be freed when '%s'"
  52.     "method returns.\nResults in a %lu octet leak the next time"
  53.     " this method is called.\n\n", aStr, sel_getName(_cmd),
  54.  
  55.     (strlen(aStr) +1));
  56.  
  57.  
  58. // According to NeXT Developer Docs, General Reference, Chapter 6
  59. // Distributed Objects:
  60. //
  61. // "On the server side, space for the data is allocated, and a pointer to
  62. // the local data is received.  The server's allocated copy of the data is
  63. // local in scope and will be freed by the system when the server's method
  64. // returns."
  65. //
  66. // As of NEXSTSTEP 3.0, this does NOT seem to be the case, so we will
  67. // explicitely free the data here:
  68.  
  69. // Uncomment the next line to fix the memory leak in 3.0
  70. // free((char *)aStr);    //??? This should NOT be required, but is...
  71.  
  72.     return self;
  73. }
  74.  
  75. void main(int argc, char **argv)
  76. {
  77.     MyServer *server;
  78.  
  79.  
  80.     char buffer[2048];
  81.     id roserver = [NXConnection connectToName:RO_NAME];
  82.     char *str;
  83.  
  84.  
  85.     if (roserver)
  86.     {
  87.     [roserver setProtocolForProxy:@protocol(ServerProtocol)];
  88.     fprintf(stderr, "Connected to server\n");
  89.     fprintf(stderr, "Enter text to send (q to quit):\n");
  90.     while (str = gets(buffer))
  91.     {
  92.         if ((strlen(str) == 1) && (str[0] == 'q')) break;
  93.         [roserver receiveStr:str];
  94.         fprintf(stderr, "Sent '%s' to server\n\n", str);
  95.         fprintf(stderr, "Enter text to send:\n");
  96.     }
  97.     } else {
  98.     server = [[MyServer alloc] init];
  99.     roserver = [NXConnection registerRoot:server withName:RO_NAME];
  100.     fprintf(stderr, "Server waiting to receive strings...\n\n");
  101.     [roserver run];
  102.     }
  103.     exit(0);
  104. }
  105.